Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
What's the job of "return" right underneath the if clause in this exact function? also pls explain in words how this function works ty

Write a function that returns the smallest number that’s greater or equal to 0. Especially that return right underneath if (num < 0). What's that return's job here?

function minNonNegative(numArray) {
  let min = Infinity;

  numArray.forEach(function(num) {
    if (num < 0) {
      return;
    } else if (num < min) {
      min = num
    }
  });
  return min;
}

console.log(minNonNegative([-3,2,5,-9]))

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The return INSIDE the forEach is to ignore the else which is then not even needed:

numArray.forEach(num => { if (num < 0) return; if (num < min) { min = num } });

You can use a filter and Math.min:

const minNonNegative = numArray => Math.min(...numArray.filter(num => num>=0))


console.log(minNonNegative([-3,2,5,-9]))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda